/*
 * Copyright (C) 2021 The Chinese Software International Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.azoft.carousellayoutmanager;

import ohos.agp.components.Component;

import java.util.List;

/**
 * item的点击事件的监听
 *
 * @since 2021-03-24
 */
public abstract class CarouselChildSelectionListener {
    private static final int INDEX = 2;

    CarouselChildSelectionListener(final CarouselLayoutManager carouselLayoutManager) {
        List<Component> allData = carouselLayoutManager.getDataComponents();
        List<Component> addData = carouselLayoutManager.getAddComponents();
        int maxVisible = carouselLayoutManager.getVisibleMaxNum();
        for (int ii = 0; ii < allData.size(); ii++) {
            int finalJ = ii;
            allData.get(ii).setClickedListener(component -> {
                clickListener(carouselLayoutManager, finalJ, allData, addData, maxVisible);
            });
        }
    }

    /**
     * 点击事件
     *
     * @param carouselLayoutManager 布局管理器
     * @param finalJ 下标
     * @param allData 数据
     * @param addData 添加的数据
     * @param maxVisible 最大显示的数量
     */
    private void clickListener(final CarouselLayoutManager carouselLayoutManager,
                               final int finalJ, final List<Component> allData,
                               final List<Component> addData, final int maxVisible) {
        if (carouselLayoutManager.getOrientation() == Component.HORIZONTAL) {
            if (carouselLayoutManager.isHorizontalLeft() || carouselLayoutManager.isHorizontalRight()) {
                carouselLayoutManager.setHorizontalLeft(false);
                carouselLayoutManager.setHorizontalRight(false);
            } else {
                int center = carouselLayoutManager.getIndex();
                if (center < finalJ) {
                    carouselLayoutManager.setBack(true);
                    carouselLayoutManager.setHorizontalLeft(true);
                    carouselLayoutManager.setHorizontalBoundary(true);
                } else if (center > finalJ) {
                    carouselLayoutManager.setBack(false);
                    carouselLayoutManager.setHorizontalRight(true);
                    carouselLayoutManager.setHorizontalBoundary(false);
                } else {
                    onCenterItemClicked(finalJ);
                    return;
                }
                onBackItemClicked(carouselLayoutManager, finalJ);
            }
        } else if (carouselLayoutManager.getOrientation() == Component.VERTICAL) {
            if (carouselLayoutManager.isVerticalTop() || carouselLayoutManager.isVerticalBottom()) {
                carouselLayoutManager.setVerticalTop(false);
                carouselLayoutManager.setVerticalBottom(false);
            } else {
                int click = 0;
                for (int jj = 0; jj < addData.size(); jj++) {
                    if (addData.get(jj) == allData.get(finalJ)) {
                        click = jj;
                        break;
                    }
                }
                if (click > maxVisible / INDEX) {
                    carouselLayoutManager.setBack(false);
                    carouselLayoutManager.setVerticalTop(true);
                } else if (click < maxVisible / INDEX) {
                    carouselLayoutManager.setBack(true);
                    carouselLayoutManager.setVerticalBottom(true);
                } else if (click == maxVisible / INDEX) {
                    onCenterItemClicked(finalJ);
                    return;
                }
                onBackItemClicked(carouselLayoutManager, finalJ);
            }
        }
    }

    /**
     * 点击中心item
     *
     * @param position 中心item的下标
     */
    protected abstract void onCenterItemClicked(int position);

    /**
     * 点击两旁的item
     *
     * @param carouselLayoutManager 布局管理器
     * @param position 点击的下标
     */
    protected abstract void onBackItemClicked(CarouselLayoutManager carouselLayoutManager, int position);
}
